﻿using System;
using System.Windows;
using System.Reflection;

namespace XAMLStylePad.Models
{
    /// <summary>
    /// Assembly as source
    /// </summary>
    public class AssemblySource : SourceBase
    {
        public Assembly _assembly;

        public AssemblySource()
        {
        }

        public AssemblySource(string fullPath)
            : base(fullPath)
        {
            Load();
            //Path = Path.Length == Name.Length ? "" : Path.Remove(Path.Length - Name.Length - 1);
        }

        public AssemblySource(Assembly asm)
            : base(asm.Location)
        {
            _assembly = asm;
            Load();
        }

        public override void Load()
        {
            this.Resources.Clear();

            try
            {
                var loader = new XAMLResourcesLoader();
                loader.LoadAssemblyByPath(FullPath);
                var list = loader.LoadResources();

                foreach(var res in list)
                {
                    this.Resources.Add(new ResourceDictionaryModel(res.Value as ResourceDictionary)
                    {
                        FileName = res.Key
                    });
                }
            }
            catch(Exception err)
            {
                XAMLStylePadMainWindow.Instance.PreviewPanel.PreviewErrorMessage(err);
            }

            IsLoaded = true;
        }

        public override bool IsReadOnly
        {
            get
            {
                return true;
            }
        }

        public override void Save(string path)
        {
            //nothing to do
        }
    }
}